package rabbitmqConsummer.consummers;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.logging.Logger;

@RabbitListener(queues = "basic_queue")
@Component
public class BasicQueueController {
    Logger logger = Logger.getLogger(BasicQueueController.class.getName());
    // 自动确认 放开
//    @RabbitHandler
//    public void receiveMessage(String message ) throws IOException {
//             System.out.println("deliveryTag: ");
//             if (1 == 1) {
//                 throw new RuntimeException("Error");
//             }
//             System.out.println("Message received: " + message);
//    }


    // 手动确认 放开
    @RabbitHandler
    public void receiveMessage(String message, Channel channel, @Header("amqp_deliveryTag") long deliveryTag) throws IOException {
        try {
            System.out.println("deliveryTag: ");
            if (1 == 1) {
                throw new RuntimeException("Error");
            }
        System.out.println("Message received: " + message);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, true);
        }
    }


    @RabbitHandler
    public void receiveMessageInteger(Integer message) {
        System.out.println("Message received: " + message);
    }
}
